﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;

/// <summary>
/// JScript 的摘要说明
/// 在开发过程中可以用的一些技巧文件
/// </summary>
public class JScript
{

    protected static HttpResponse Response;
    protected static HttpSessionState Session;
	public JScript()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}


    /// <summary>
    /// 弹出JavaScript小窗口
    /// </summary>
    /// <param name="js">窗口信息</param>
    public static void Alert(string message)
    {
        message = StringUtil.DeleteUnVisibleChar(message);
        string js = @"<Script language='JavaScript'>
                    alert('" + message + "');</Script>";
        HttpContext.Current.Response.Write(js);
    }
    public static void Alert(object message)
    {
        string js = @"<Script language='JavaScript'>
                    alert('{0}');  
                  </Script>";
        HttpContext.Current.Response.Write(string.Format(js, message.ToString()));
    }


    /// <summary>
    /// 判断Session中某键值是否存在，如果不存在，输出提示信息
    /// 并且转向相应页面
    /// </summary>
    /// <param name="SessionName">Session中键名称</param>
    /// <param name="Message">提示信息</param>
    /// <param name="RedirectUrl">要转向的页面</param>
    public static void CheckSession(string SessionName, string Message, string RedirectUrl)
    {
        Session = HttpContext.Current.Session;
        Response = HttpContext.Current.Response;
        if (Session[SessionName] == null||Session[SessionName].ToString()=="")
        {
            WriteMessage(Message, false, false);
            string js = "top.document.location='" + RedirectUrl + "';";
            WriteJavaScript(js);
            return;
        }
    }


    public static void WriteMessage(string strMsg, bool Back, bool End)
    {
        Response = HttpContext.Current.Response;
        //将单引号替换，防止javascript出错
        strMsg = strMsg.Replace("'", "“");
        //将回车换行符去掉，防止javascript出错
        strMsg = strMsg.Replace("\r\n", "");
        Response.Write("<script language=javascript>alert('" + strMsg + "');</script>");
        if (Back)
            Response.Write("<script language=javascript>history.back();</script>");
        if (End)
            Response.End();
    }


    /// <summary>
    /// 以Response.Write()方式输出一条JavaScript语句
    /// </summary>
    /// <param name="strJavaScript">要输出的JavaScript语句</param>
    public static void WriteJavaScript(string strJavaScript)
    {
        Response = HttpContext.Current.Response;
        Response.Write("<script language=javascript>" + strJavaScript + "</script>");
    }



    /// <summary>
    /// 弹出JavaScript小窗口,并转向指定的页面
    /// </summary>
    /// <param name="message">弹出信息</param>
    /// <param name="toURL">专转向的网页</param>
    public static void AlertAndRedirect(string message, string toURL)
    {
        string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
        HttpContext.Current.Response.Write(string.Format(js, message, toURL));
    }

}
